clear all
macro drop _all
set more off
set type double
set maxvar 10000

use main_hwol_data

/*drop DE from the dataset all-together */
drop if state_code=="10"

/* ban  = 1 for states after they pass the ban -  See Table 1 for dates*/
gen ban = 0

replace ban= 1 if (state_code=="06" & date>=tq(2012q1)) | ///
(state_code=="08" & date>=tq(2013q3)) | (state_code=="09" & date>=tq(2011q4)) | ///
(state_code=="10" & date>=tq(2014q2)) | (state_code=="15" & date>=tq(2009q3)) | ///
(state_code=="17" & date>=tq(2011q1)) | (state_code=="24" & date>=tq(2011q4)) | ///
(state_code=="32" & date>=tq(2013q4)) | (state_code=="41" & date>=tq(2010q2)) | ///
(state_code=="50" & date>=tq(2012q3)) | (state_code=="53" & date>=tq(2007q3))

gen exempt = 0
replace exempt = 1 if SOC_2digit == "13" | ///
SOC_2digit == "23" | SOC_2digit == "33"

* replace exempt = 1 if SOC_2digit =="11" We use this only for HI and NV below

replace exempt = 1 if (SOC_2digit =="11" & state_code=="15") | ///
( SOC_2digit =="11" & state_code=="32") 

* Generate the interaction term udnerlying the triple-diff specification
gen ban_affected = ban*(1-exempt)

* Log transformation
gen ln_vol = ln(volume)
gen ln_volnew = ln(volumenew)
gen inv_hysine = ln(volume + sqrt(volume^2 +1))

* Define fixed effects
egen county_occ_fe = group(code SOC_2digit)
egen county_time_fe = group(code date)
egen time_occ_fe = group(date SOC_2digit)

* Panel definition
sort county_occ_fe date
xtset county_occ_fe date

// Ban duration dummies  used in the Distrubted lag specification 
gen ban_duration_0 = 0

//For first obs where county gets ban
replace ban_duration_0= 1 if (state_code=="06" & date==tq(2012q1)) | ///
(state_code=="08" & date==tq(2013q3)) | (state_code=="09" & date==tq(2011q4)) | ///
(state_code=="10" & date==tq(2014q2)) | (state_code=="15" & date==tq(2009q3)) | ///
(state_code=="17" & date==tq(2011q1)) | (state_code=="24" & date==tq(2011q4)) | ///
(state_code=="32" & date==tq(2013q4)) | (state_code=="41" & date==tq(2010q2)) | ///
(state_code=="50" & date==tq(2012q3)) | (state_code=="53" & date==tq(2007q3))

gen ban_duration_1 = 0 
//For quarter+1 from when the law was passed
replace ban_duration_1 = 1 if l.ban_duration_0==1

gen ban_duration_2 = 0 
//For quarter+2
replace ban_duration_2 = 1 if l.ban_duration_1==1

gen ban_duration_3 = 0
replace ban_duration_3 = 1 if l.ban_duration_2==1

gen ban_duration_4 = 0
replace ban_duration_4 = 1 if l.ban_duration_3==1

gen ban_duration_5 = 0 
//For all further observations (passed the first year)
replace ban_duration_5 = 1 if l.ban_duration_4==1
replace ban_duration_5 = l.ban_duration_5 if ban_duration_5==0 & l.ban_duration_5==1

//Now same thing counting backwards, count back four quarters
//Previous 4 quaters 

gen ban_duration_neg1 = 0
replace ban_duration_neg1 = 1 if f.ban_duration_0==1

gen ban_duration_neg2 = 0
replace ban_duration_neg2 = 1 if f.ban_duration_neg1==1

gen ban_duration_neg3 = 0
replace ban_duration_neg3 = 1 if f.ban_duration_neg2==1

gen ban_duration_neg4 = 0
replace ban_duration_neg4 = 1 if f.ban_duration_neg3==1

// Create the exempt interaction

gen ban_affected_neg1 = ban_duration_neg1*(1-exempt)
gen ban_affected_neg2 = ban_duration_neg2*(1-exempt)
gen ban_affected_neg3 = ban_duration_neg3*(1-exempt)
gen ban_affected_neg4 = ban_duration_neg4*(1-exempt)

gen ban_affected_5 = ban_duration_5*(1-exempt)
gen ban_affected_4 = ban_duration_4*(1-exempt)
gen ban_affected_3 = ban_duration_3*(1-exempt)
gen ban_affected_2 = ban_duration_2*(1-exempt)
gen ban_affected_1 = ban_duration_1*(1-exempt)
gen ban_affected_0 = ban_duration_0*(1-exempt)

* Baseline triple diff using inverse hyperbolic sine function -  Appendix
* Column 1 of Table A4
reghdfe inv_hysine ban_affected, absorb(county_occ_fe county_time_fe time_occ_fe) ///
vce(cluster state_code SOC_2digit)
outreg2 using table_A4.xls, dec(3) replace

* Column 1 of table A5
reghdfe inv_hysine ban_affected_neg4 ban_affected_neg3 ban_affected_neg2 ban_affected_neg1 ///
ban_affected_0 ban_affected_1 ban_affected_2 ban_affected_3 ban_affected_4 ban_affected_5 ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A5.xls, dec(3) replace

* Add Regressions relating to Suprime fraction of a county and the task content of the occupations
gen subfrac_ban_affected = subfraction_2005*ban_affected
* Rescale other subprime moments to use in the regression below
sum riskscore_sd
/*
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
riskscore_sd |  3,382,886     101.241    11.82062   .7071068   279.3072
*/
replace riskscore_sd = riskscore_sd/11.82062
gen subsd_ban_affected = riskscore_sd*ban_affected

gen risk_9010 = riskscore_p90-riskscore_p10

sum risk_9010
/*
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
   risk_9010 |  3,386,474    260.0859    36.12876          0        501
*/

replace risk_9010 = risk_9010/36.12876
gen risk_disp_ban_affected = risk_9010*ban_affected

* Column 1 of Table A6
reghdfe inv_hysine subfrac_ban_affected ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A6.xls, dec(3) replace

gen ban_subfrac_neg1 = ban_duration_neg1*subfraction_2005*(1-exempt)
gen ban_subfrac_neg2 = ban_duration_neg2*subfraction_2005*(1-exempt)
gen ban_subfrac_neg3 = ban_duration_neg3*subfraction_2005*(1-exempt)
gen ban_subfrac_neg4 = ban_duration_neg4*subfraction_2005*(1-exempt)

gen ban_subfrac_5 = ban_duration_5*subfraction_2005*(1-exempt)
gen ban_subfrac_4 = ban_duration_4*subfraction_2005*(1-exempt)
gen ban_subfrac_3 = ban_duration_3*subfraction_2005*(1-exempt)
gen ban_subfrac_2 = ban_duration_2*subfraction_2005*(1-exempt)
gen ban_subfrac_1 = ban_duration_1*subfraction_2005*(1-exempt)
gen ban_subfrac_0 = ban_duration_0*subfraction_2005*(1-exempt)

* Column 2 of table A5
reghdfe inv_hysine ban_subfrac_neg4 ban_subfrac_neg3 ban_subfrac_neg2 ban_subfrac_neg1 ///
ban_subfrac_0 ban_subfrac_1 ban_subfrac_2 ban_subfrac_3 ban_subfrac_4 ban_subfrac_5 ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A5.xls, dec(3) append

* Details about the Occupational task Content - Routine etc.

gen routine_m = 0
replace routine_m = 1 if (SOC_2digit =="47" | SOC_2digit=="49" | ///
SOC_2digit =="51" | SOC_2digit=="53" ) 

gen routine_c = 0
replace routine_c = 1 if (SOC_2digit =="41" | SOC_2digit=="43")

gen non_routine_c = 0
replace non_routine_c = 1 if (SOC_2digit =="11" | SOC_2digit=="13" | ///
SOC_2digit =="15" | SOC_2digit=="17" | SOC_2digit =="19" | SOC_2digit=="21" | ///
SOC_2digit =="23" | SOC_2digit=="25" | SOC_2digit =="27" | SOC_2digit=="29" )

gen non_routine_m = 0
replace non_routine_m = 1 if (SOC_2digit =="31" | SOC_2digit=="33" | /// 
SOC_2digit =="35" | SOC_2digit=="37" | SOC_2digit =="39")

gen routine = 0
replace routine = 1 if (routine_m == 1| routine_c == 1)
gen non_routine = 0
replace non_routine = 1 if (non_routine_m ==1 | non_routine_c == 1)

gen ban_affect_routine = ban_affected*routine
gen ban_routine = ban*routine
gen ban_routine_m = ban*routine_m
gen ban_routine_c = ban*routine_c
gen ban_affect_nonroutine = ban_affected*non_routine

*Column 3 of table A6
reghdfe inv_hysine ban_affect_routine , absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A6.xls, dec(3) append

* Leads and Lags
gen ban_routine_neg1 = ban_duration_neg1*routine
gen ban_routine_neg2 = ban_duration_neg2*routine
gen ban_routine_neg3 = ban_duration_neg3*routine
gen ban_routine_neg4 = ban_duration_neg4*routine

gen ban_routine_5 = ban_duration_5*routine
gen ban_routine_4 = ban_duration_4*routine
gen ban_routine_3 = ban_duration_3*routine
gen ban_routine_2 = ban_duration_2*routine
gen ban_routine_1 = ban_duration_1*routine
gen ban_routine_0 = ban_duration_0*routine

*Column 4 of table A5
reghdfe inv_hysine ban_routine_neg4 ban_routine_neg3 ban_routine_neg2 ban_routine_neg1 ///
ban_routine_0 ban_routine_1 ban_routine_2 ban_routine_3 ban_routine_4 ban_routine_5 ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A5.xls, dec(3) append

**  TABLE A7 and A8
* a) EXPOSURE TO BAN Table A7

reghdfe inv_hysine subfrac_ban_affected ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A7.xls, dec(3) append

reghdfe inv_hysine subsd_ban_affected ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A7.xls, dec(3) append

reghdfe inv_hysine risk_disp_ban_affected ///
, absorb(county_occ_fe county_time_fe time_occ_fe) vce(cluster state_code SOC_2digit)
outreg2 using table_A7.xls, dec(3) append

* B) TABLE A8
* Routine Detail
reghdfe inv_hysine ban_affect_nonroutine ban_routine, absorb(county_occ_fe county_time_fe time_occ_fe) /// 
vce(cluster state_code SOC_2digit)
outreg2 using table_A8.xls, dec(3) replace

reghdfe inv_hysine ban_affect_nonroutine ban_routine_m ban_routine_c, absorb(county_occ_fe county_time_fe time_occ_fe) /// 
vce(cluster state_code SOC_2digit)
outreg2 using table_A8.xls, dec(3) append

* Interactions
gen interaction_1 = ban_affect_nonroutine*subfraction_2005
gen interaction_2 = ban_routine*subfraction_2005
gen interaction_3 = ban_routine_m*subfraction_2005
gen interaction_4 = ban_routine_c*subfraction_2005

reghdfe inv_hysine interaction_1 interaction_2, absorb(county_occ_fe county_time_fe time_occ_fe) /// 
vce(cluster state_code SOC_2digit)
outreg2 using table_A8.xls, dec(3) append

reghdfe inv_hysine interaction_1 interaction_3 interaction_4, absorb(county_occ_fe county_time_fe time_occ_fe) /// 
vce(cluster state_code SOC_2digit)
outreg2 using table_A8.xls, dec(3) append


